package jtk.project4.fleet.screen.addEditNewEmployee;

import java.awt.FlowLayout;
import java.util.List;

import jtk.project4.fleet.action.CancelEmployeesTaskAction;
import jtk.project4.fleet.action.SaveEmployeesTask;
import jtk.project4.fleet.event.EmployeeEvent;
import jtk.project4.fleet.field.DriverLicenseInformationField;
import jtk.project4.fleet.field.GeneralInformationField;
import jtk.project4.fleet.field.IdentificationField;
import jtk.project4.fleet.field.MicellaneousField;
import jtk.project4.fleet.field.PersonnelInformationField;
import jtk.project4.fleet.field.EmployeeContactAddressField;
import jtk.project4.fleet.field.EmployeeContactPhonesNumberEmailField;
import jtk.project4.fleet.field.EmployeeContactNotesField;
import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.form.FormField;
import nl.coderight.jazz.form.FormLayout;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.control.TabControl;
import nl.coderight.jazz.form.control.TabItem;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;

public class AddEditNewEmployeeView extends FormView{
	private TabControl employeeTab;
	private TabItem generalTab;
	private TabItem contactTab;
	private TabItem IdentificationTab;
	private TabItem photoTab;

	private Action submitAction;
	private Action cancelAction;
	private PushButton submitButton;
	private PushButton cancelButton;

	public void handleEvent(EmployeeEvent evt){
		switch(evt.getType()){
		case CANCEL :
			close();
			break;
		case SAVE :
			List<ValidationError> errors = validate();
			if(!errors.isEmpty()){
				errors.get(0).getField().focus();
				break;
			}else{
				submit();
				close();
			}
		default :
			propagateEvent(evt);
			break;				
		}
		
	}
	@Override
	protected void init() {
		// TODO Auto-generated method stub
		createTabFields();		
		createActions();
		createFields();
		createLayout();
	}
	
	private void createLayout() {
		addField(createForm());
	}
	
	
	private void createActions() {
		submitAction = new SaveEmployeesTask();
		cancelAction = new CancelEmployeesTaskAction();
	}
	
	private void createFields() {
		submitButton = new PushButton(submitAction);
		cancelButton = new PushButton(cancelAction);
	}
	
	private void createTabFields()
	{
		employeeTab = new TabControl();
		generalTab = new TabItem("General");
		contactTab = new TabItem("Contact");
		IdentificationTab = new TabItem("Identification");
		//photoTab = new TabItem("Photo");
	}
	
	private FormField createTabs()
	{
		TabControl form = employeeTab;
		employeeTab.addTab(generalTab, createGeneralTab());
		employeeTab.addTab(contactTab, createContactTab());
//		form.addTab(IdentificationTab, createIdentificationTab());
		form.addTab(photoTab, createGeneralTab());
		return employeeTab;
	}
	
	private GroupControl createGeneralTab() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		.addField(new GeneralInformationField("employeess"))
		.addRow()
		.addField(new PersonnelInformationField("employeess"))
		.addRow()
		.addField(new DriverLicenseInformationField("employeess"))
		.addRow()
		.addField(new MicellaneousField("employeess"))
		;				
		
		return form;
	}
	
	private GroupControl createContactTab() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		.addField(new EmployeeContactAddressField("employeess"))	
		.addRow()
		.addField(new EmployeeContactPhonesNumberEmailField("employeess"))
		.addRow()
		.addField(new EmployeeContactNotesField("employeess"))		
		;				
		
		return form;
	}
//	
//private GroupControl createIdentificationTab() {
//		GroupControl form = new GroupControl();
//		form.setLayout(new FormLayout())
//		.addField(new IdentificationField("identification"))
//		;						
//		return form;
//	}
	
	private FormField createForm()
	{
		GroupControl form = new GroupControl();
		
		form.addField(createTabs());
		form.addField(createButtons(), SOUTH);
		
		return form;
	}
	
	private GroupControl createGeneralForm() {
		GroupControl form = new GroupControl();
		form.addField(createGeneralForm(), NORTH);
		form.addField(createButtons(), SOUTH);
		return form;
	}
	
	private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(submitButton);
		buttons.addField(cancelButton);
		return buttons;
	}
	
	
}
